<?php

class Model_MemberApplication extends Model{

	function __construct(){
		$this->Lookup = getModel('Model_Lookup');
	}

	/* 獲得產品報告列表資料

	* $type int 1已提交報告 2未提交報告 0或者不填 全部
	* $mid int 會員ID
	* $orderby		string	 	字段名 desc 或 字段名 asc
	* $page		int			當前頁數
	* $size		int			獲取個數
	* return		array		資料數組
	*/
	function getMyApplicationList($mid=0,$type=0,$orderby="TB1.id desc",$page = 1, $size = 4 ){
		if(!intval($mid)) return false;
		$where="TB1.status=1 and TB1.mid=".intval($mid);
		switch ($type) {
			case 1:
				$where .= " and TB1.is_submitted=1 and (TB2.is_accept=1 or (TB2.is_accept=0 and TB2.reject_reason=0))";
				break;
			case 2:
				$where .= " and TB1.is_submitted=0 and (TB2.is_accept=1 or (TB2.is_accept=0 and TB2.reject_reason=0))";
				break;
                        case 3:
				$where .= " and TB1.is_submitted=1 and TB2.is_accept=0 and TB2.reject_reason<>0";
				break;
			case 0:
				$where .= "  and (TB2.is_accept=1 or (TB2.is_accept=0 and TB2.reject_reason=0))";
				break;
			default:
				return  false;
				break;
		}
		$param = array();
		$param['field'] = "TB1.id as application_id,TB1.product ,TB1.mid ,TB1.report_endtime as my_report_endtime ,TB1.is_express_delay,TB1.is_member_delay,TB1.is_submitted ,TB1.rid";
		$param['where'] = $where." and  TB1.rid not in (select TB2.id from product_reports where TB2.is_accept=1 and TB2.status=1)";
		$param['order'] = $orderby; //"id asc"
		$param['group'] = "";
		$param['limit'] = "";
		$param['pagesize'] = $size;
		$param['page'] = $page;
		$param['join'] = "LEFT JOIN product_reports";
		$param['on'] = "TB1.rid = TB2.id";
		$data = db_query_hash('member_application',$param);



		$brand = $this->Lookup->getLookup('brand');

		$brandarr = array();
		if($brand['result']){
			foreach ($brand['result'] as $value) {
				$brandarr[$value['id']]=$value['name'];
			}
		}

		$result = array();
		if($data['result']){
			foreach ($data['result'] as $key => $value) {
				$param = array();
				$param['field'] = "id,apply_endtime,reports,name,cname,brand,product_type,quota,body,about_url,cover_image1 as cover,is_hightlight,member_type,apply_starttime,report_starttime,report_endtime,price_trial,capacity_trial,product_layout_category,(apply_num + apply_num_offset) as apply_num ,(click + click_offset) as click,(likes + likes_offset) as likes";
				$param['where'] = "id =".$value['product'];
				$param['order'] = ""; //"id asc"
				$param['group'] = "";
				$param['limit'] = "";
				$param['pagesize'] = 1;
				$param['page'] = 1;
				$param['join'] = "";
				$param['on'] = "";
				$data_product = db_query_hash('product',$param);


				if($data_product['result']) $value = array_merge($value,$data_product['result'][0]);

				if($value['brand']) $value['type_name'] = $brandarr[$value['brand']];
				$result[$key]=$value;
			}
		}

		$data['result']=$result;


		return $data;
	}
}
?>